from django.db import models

class Books(models.Model):
    title = models.CharField(max_length=255)
    description = models.TextField()
    main_page = models.TextField()

    class Meta():
        db_table = "books"

class News(models.Model):
    title = models.CharField(max_length=255)
    short_description = models.TextField()
    text = models.TextField()
    date_create = models.DateField()

    class Meta():
        db_table = "news"

class Themes(models.Model):
    book_id = models.IntegerField()
    parent = models.ForeignKey(Books)
    prev_id = models.IntegerField()
    next_id = models.IntegerField()
    is_title = models.IntegerField()
    visible = models.IntegerField()
    title = models.CharField(max_length=255)
    text = models.TextField()

    class Meta():
        db_table = "theme"

class DataHandler():
    def build_json_response(self, data):
        result = []
        for row in data:
            node = {}
            node = {
                "id": row.id,
                "text": row.title,
                "parent": "false"
            }
            if row.is_title == 1:
                node["hasChildren"] = "true"
                node["parent"] = "true"

            result.append(node)
        return result
